package com.api.demo.controller;

import com.api.demo.dto.CommentRequest;
import com.api.demo.entity.Comment;
import com.api.demo.entity.User;
import com.api.demo.service.CommentService;
import com.api.demo.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/content/{contentId}/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @PostMapping
    public ResponseEntity<?> createComment(
            @PathVariable Long contentId,
            @RequestBody CommentRequest request,
            Authentication authentication) {
        Comment comment = commentService.createComment(
                contentId,
                request,
                getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok(comment);
    }

    @PostMapping("/{commentId}/reply")
    public ResponseEntity<?> replyComment(
            @PathVariable Long contentId,
            @PathVariable Long commentId,
            @RequestBody CommentRequest request,
            Authentication authentication) {
        Comment comment = commentService.replyComment(
                contentId,
                commentId,
                request,
                getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok(comment);
    }

    @GetMapping
    public ResponseEntity<?> getCommentList(
            @PathVariable Long contentId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Authentication authentication) {
        IPage<Comment> commentPage = commentService.getCommentList(
                contentId,
                new Page<>(page, size),
                getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok(commentPage);
    }

    @PostMapping("/{commentId}/like")
    public ResponseEntity<?> likeComment(
            @PathVariable Long contentId,
            @PathVariable Long commentId,
            Authentication authentication) {
        commentService.likeComment(commentId, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{commentId}/like")
    public ResponseEntity<?> unlikeComment(
            @PathVariable Long contentId,
            @PathVariable Long commentId,
            Authentication authentication) {
        commentService.unlikeComment(commentId, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{commentId}")
    public ResponseEntity<?> deleteComment(
            @PathVariable Long contentId,
            @PathVariable Long commentId,
            Authentication authentication) {
        commentService.deleteComment(commentId, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    private Long getUserIdFromUsername(String username) {
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }
        return user.getId();
    }
}